package org.six11.olive3.picturephone.server.handler;

import java.io.File;
import java.util.List;
import static org.six11.olive3.server.DatabaseThing.*;

import javax.servlet.ServletException;

import org.six11.olive3.net.Message;
import org.six11.olive3.net.MessageHandler;
import org.six11.olive3.net.MessageNames;
import org.six11.olive3.picturephone.server.PPMessageNames;
import org.six11.olive3.server.DatabaseThing;
import org.six11.olive3.server.SqlStrings;
import org.six11.olive3.util.MultiState;
import org.six11.olive3.util.ImageUtil;

/**
 * 
 * 
 * @author Gabe Johnson <johnsogg@cmu.edu>
 */
public class SketchDoneMessageHandler extends MessageHandler {

  private DatabaseThing dbThing;
  private MessageCacheHandler sketchHandler;
  private MultiState programState;
  
  public SketchDoneMessageHandler(DatabaseThing dbThing, MultiState programState, MessageCacheHandler sketchHandler) {
    this.dbThing = dbThing;
    this.sketchHandler = sketchHandler;
    this.programState = programState;
  }

  public void handleLater(Message m) {
    String userID = m.getParamValue(MessageNames.PARAM_USER_ID);
    String descID = m.getParamValue(PPMessageNames.PARAM_DESC_ID);
    List<Message> sketchMessages = sketchHandler.popMessages(userID);
    List<Message> merged = Message.merge(sketchMessages);
    StringBuffer buf = new StringBuffer();
    for (Message sketchMsg : merged) {
      buf.append(sketchMsg.toOliveString());
    }
    try {
      bug("Inserting a sketch for user " + userID + " based on description " + descID);
      dbThing.insertRow(SqlStrings.INSERT_SKETCH_FOR_DESCRIPTION, userID, descID, buf.toString());
    } catch (ServletException ex) {
      ex.printStackTrace();
    }
  }

}
